package com.me.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.me.pojo.Employee;
import com.me.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    @RequestMapping("/showEmployee.do")
    public String selectEmployee(Model model,Integer id)
    {
        Employee employee =employeeService.selectById(id);
        model.addAttribute("employeemsg",employee);
        return "employee";
    }


    @RequestMapping("/toRegister.do")
    public String toregister(Model model,Integer id)

    {
        return "register";
    }
    @RequestMapping("/checkRegister.do")
    public String checkRegister(Model model, String username, HttpServletResponse response) throws  Exception
    {
        //1.获取用户名
        //2.调用service层判断用户名是否存在
        //期望服务器响应回的数据格式：{"userExsit":true,"msg":"此用户名已经存在，请重新输入"}
        //                         {"userExsit":false,"msg":"用户名可用"}
        //设置响应的数据格式为json 编码为utf-8
        response.setContentType("application/json;charset=utf-8");
        Map<String,Object> map = new HashMap<String,Object>();
        Employee employee=employeeService.selectByName(username);
        if(employee!=null){
            //存在
            map.put("userExsit",true);
            map.put("msg","此用户名已经存在，请重新输入");
        }else{
            //不存在
            map.put("userExsit",false);
            map.put("msg","用户名可用");
        }

        //将map转为json，并且传递给客户端
        //将map转为json

        ObjectMapper mapper = new ObjectMapper();
        //并且传递给客户端
        mapper.writeValue(response.getWriter(),map);
        return "register";
    }
    @RequestMapping("/Register.do")
    public String Register(Model model,int age,HttpServletRequest request,HttpServletResponse response) throws  Exception
    {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String username= new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");
        String sex= new String(request.getParameter("sex").getBytes("ISO-8859-1"),"utf-8");
        String address= new String(request.getParameter("address").getBytes("ISO-8859-1"),"utf-8");
        Employee employee=new Employee(1,username,age,sex,address);
        int result=employeeService.insertEmployee(employee);
        if (result==1) {
            System.out.println("注册成功");
            List<Employee> employees=employeeService.selectEmployee();
            model.addAttribute("employeesmsg",employees);
            return "list";
        }
        else {
            System.out.println("注册失败");
            return "register";
        }

    }
    @RequestMapping("/toList.do")
    public String tolist(Model model)
    {
        List<Employee> employees=employeeService.selectEmployee();
        model.addAttribute("employeesmsg",employees);
        return "list";
    }
    @RequestMapping("/toupdateEmployee.do")
    public String toupdateEmployee(Model model,int id)
    {
        Employee employee=employeeService.selectById(id);
        model.addAttribute("employeemsg",employee);
        return "list2";
    }
    @RequestMapping("/updateEmployee.do")
    public String updateEmployee(Model model,Integer age,Integer id,HttpServletRequest request,HttpServletResponse response)throws  Exception
    {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String username= new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");
        String sex= new String(request.getParameter("sex").getBytes("ISO-8859-1"),"utf-8");
        String address= new String(request.getParameter("address").getBytes("ISO-8859-1"),"utf-8");
        Employee employee=new Employee(id,username,age,sex,address);
        int result=employeeService.updateEmployee(employee);
        if (result==1)
        {
            JOptionPane.showMessageDialog(null, "修改成功");
            List<Employee> employees=employeeService.selectEmployee();
            model.addAttribute("employeesmsg",employees);
            return "list";
        }
        else
            JOptionPane.showMessageDialog(null, "修改失败");

        return "list";
    }

    @RequestMapping("/todeleteEmployee.do")
    public String todeleteEmployee(Model model,Integer id)
    {
        int result=employeeService.deleteEmployee(id);
        if (result==1)
        {
            JOptionPane.showMessageDialog(null, "删除成功");
            List<Employee> employees=employeeService.selectEmployee();
            model.addAttribute("employeesmsg",employees);
            return "list";
        }
        else
            JOptionPane.showMessageDialog(null, "删除失败");
        return "list";
    }

}

